//index.js
//获取应用实例
const app = getApp()
// const http = require("../../http.js");
import {
  req
} from "../../http.js";
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    slides: [{
      url: "http://p1.meituan.net/mmc/05200dedfe05e78164106a038517a5f3179251.jpg"
    },
    {
      url: "http://p0.meituan.net/mmc/039bd9e99555d84008cc46305dfeb99f105211.jpg"
    },
    {
      url: "http://p1.meituan.net/mmc/05200dedfe05e78164106a038517a5f3179251.jpg"
    }
    ],
    movies: [],
    id: '',
    active: 0,
    forwardTo: "",//期待想看
    offset: 0,
    city:""
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({

    })
  },
  getLocation(){
    wx.navigateTo({
        url:"../city/city"
    })
  },
  getList: function (e) {
    let index = e.target.dataset.current
    this.setData({
      active: index
    })
    if (index == 0) {
      var that = this;
      req("ajax/movieOnInfoList", "GET", {
        token: ""
      }, (res) => {
        if (res) {
          that.setData({
            movies: res.movieList.map((item) => {
              item.img = item.img.replace('http', 'https').replace('w.h', '128.180');
              item.sc += '';
              item['l'] = item.sc.split('.')[0] + (item.sc.split('.').length > 1 ? '.' : '');
              item['r'] = item.sc.split('.')[1];
              return item;
            }),
          })
        }
      }, (fail) => {

      })
    } else if (index == 1) {
      this.getForwardTo();
      req("ajax/comingList", "GET", {
        token: "", ci: "10", limit: 10
      }, (res) => {
        if (res) {
          this.setData({
            forwardTo: res.coming.map((item) => {
              item.img = item.img.replace('http', 'https').replace('w.h', '170.230');
              return item;
            }),
          })
        }
      }, (fail) => {

      })
    }
  },
  goDetail(event) {
    var id = event.currentTarget.id
    wx.navigateTo({
      url: 'detail/detail?id=' + id,
    })
  },
  onLoad: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        let latitude = res.latitude;
        let longitude = res.longitude;
        if(res){
          wx.request({
            url: 'https://apis.map.qq.com/ws/geocoder/v1/',
            data: { location: (latitude + "," + longitude), key:"MUKBZ-XDWKI-QNNG3-5ZGBZ-HO4PE-VAB7J"},
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'GET',
            dataType: 'json',
            success: function(res) {
              that.setData({
                city: res.data.result.address_component.city
              })
            },
            fail: function(res) {
              wx.showToast({
                title: '获取位置失败',
                icon: 'loading',
                image: '',
                duration: 0,
                mask: true,
                success: function(res) {},
                fail: function(res) {},
                complete: function(res) {},
              })
              console.log("获取失败")
            },
            complete: function(res) {},
          })
        }
      }
    })
  },
  onShow() {
    var that = this;
    req("ajax/movieOnInfoList", "GET", {
      token: ""
    }, (res) => {
      if (res) {
        that.setData({
          movies: res.movieList.map((item) => {
            item.img = item.img.replace('http', 'https').replace('w.h', '128.180');
            item.sc += '';
            item['l'] = item.sc.split('.')[0] + (item.sc.split('.').length > 1 ? '.' : '');
            item['r'] = item.sc.split('.')[1];
            return item;
          }),
        })
      }
    }, (fail) => {

    })
  },
  //近期受欢迎
  getForwardTo() {
    return new Promise((resolve) => {
      req("ajax/mostExpected", "GET", {
        token: "", ci: "10", offset: this.data.offset, limit: 10
      }, (res) => {
        if (res) {
          if (this.data.offset == 0) {
            this.setData({
              forwardTo: res.coming.map((item) => {
                item.img = item.img.replace('http', 'https').replace('w.h', '170.230');
                return item;
              })
            })

          } else {
            this.setData({
              forwardTo: this.data.forwardTo.concat(
                res.coming.map((item) => {
                  item.img = item.img.replace('http', 'https').replace('w.h', '170.230');
                  return item;
                })
              )
            })
          }
          resolve(true);
        }
        if (this.data.offset === 0) {
          this.data.offset += 10;
        }
      }, (fail) => {
        resolve(false);
      })
    })
  },
  //加载更多期待
  lower() {
    this.getForwardTo().then((res) => {
      if (res) {
        this.data.offset = this.data.offset + 10;
      }
    })
  }
})